/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:11 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class Nguoidung_nhomBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Nguoidung_nhom Này";
		private static string EX_FK_INGUOIDUNGID_INVALID="FK_iNguoidungID không hợp lệ";
		private static string EX_FK_INHOMNGUOIDUNGID_INVALID="FK_iNhomnguoidungID không hợp lệ";
		private static string EX_ID_INVALID="FK không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Nguoidung_nhom Theo ID
        /// </summary>
        /// <param name="FK_iNguoidungID">Int64:Nguoidung_nhom ID</param>
        /// <returns>Nguoidung_nhomEntity</returns>        
        public static List<Nguoidung_nhomEntity> GetByFK_iNguoidungID(Int64 FK_iNguoidungID)
        {
            
			if(FK_iNguoidungID<=0)
				throw new Exception(EX_ID_INVALID);
            return Nguoidung_nhomDAL.GetByFK_iNguoidungID(FK_iNguoidungID);
        }
        public static List<Nguoidung_nhomEntity> GetByGetBysKyHieuNhomnguoidung(String sKyhieu)
        {
            return Nguoidung_nhomDAL.GetByGetBysKyHieuNhomnguoidung(sKyhieu);
        }
        public static List<Nguoidung_nhomEntity> GetByFK_iNhomnguoidungID(Int64 FK_iNhomnguoidungID)
        {

            if (FK_iNhomnguoidungID <= 0)
                throw new Exception(EX_ID_INVALID);
            return Nguoidung_nhomDAL.GetByFK_iNhomnguoidungID(FK_iNhomnguoidungID);
        }
        public static Nguoidung_nhomEntity GetByFK_iNguoidungIDAndFK_iNhomnguoidungID(Int64 FK_iNguoidungID, Int64 FK_iNhomnguoidungID)
        {
            if (FK_iNhomnguoidungID <= 0)
                throw new Exception(EX_ID_INVALID);
            return Nguoidung_nhomDAL.GetByFK_iNguoidungIDAndFK_iNhomnguoidungID(FK_iNguoidungID, FK_iNhomnguoidungID);
        }
        /// <summary>
        /// Lấy về List các Nguoidung_nhom
        /// </summary>
        /// <returns>List Nguoidung_nhomEntity:List Nguoidung_nhom Cần lấy</returns>
        public static List<Nguoidung_nhomEntity> GetAll()
        {
            return Nguoidung_nhomDAL.GetAll();
        }
        
        /// <summary>
        /// Kiểm tra và thêm mới Nguoidung_nhom
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Nguoidung_nhom Mới Thêm Vào</returns>
        public static Int32 Add(Nguoidung_nhomEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return Nguoidung_nhomDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Nguoidung_nhom
        /// </summary>
        /// <param name="entity">Nguoidung_nhomEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(Nguoidung_nhomEntity entity)
        {
            checkExist(entity.FK_iNguoidungID, entity.FK_iNhomnguoidungID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return Nguoidung_nhomDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Nguoidung_nhom
        /// </summary>
        /// <param name="FK_iNguoidungID">Int64 : FK_iNguoidungID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 FK_iNguoidungID, Int64 FK_iNhomnguoidungID)
        {
            checkExist(FK_iNguoidungID, FK_iNhomnguoidungID);
            return Nguoidung_nhomDAL.Remove(FK_iNguoidungID, FK_iNhomnguoidungID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 FK_iNguoidungID, Int64 FK_iNhomnguoidungID)
        {
            Nguoidung_nhomEntity oNguoidung_nhom=Nguoidung_nhomDAL.GetByFK_iNguoidungIDAndFK_iNhomnguoidungID(FK_iNguoidungID, FK_iNhomnguoidungID);
            if(oNguoidung_nhom==null)
                throw new Exception("Không tồn tại FK_iNhomgnuoidungID và FK_iNguoidungID này!");
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">Nguoidung_nhomEntity: entity</param>
        private static void checkLogic(Nguoidung_nhomEntity entity)
        {   
			if (entity.FK_iNguoidungID < 0)
				throw new Exception(EX_FK_INGUOIDUNGID_INVALID);
			if (entity.FK_iNhomnguoidungID < 0)
				throw new Exception(EX_FK_INHOMNGUOIDUNGID_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">Nguoidung_nhomEntity: Nguoidung_nhomEntity</param>
        private static void checkDuplicate(Nguoidung_nhomEntity entity,bool checkPK)
        {
            /* 
            Example
            List<Nguoidung_nhomEntity> list = Nguoidung_nhomDAL.GetAll();
            if (list.Exists(
                delegate(Nguoidung_nhomEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.FK_iNguoidungID != entity.FK_iNguoidungID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">Nguoidung_nhomEntity:entity</param>
        private static void checkFK(Nguoidung_nhomEntity entity)
        {            
        }
        #endregion
    }
}
